
package com.yimixian.app.store;

import com.yimixian.app.R;
import com.yimixian.app.model.Store;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class StoreItemView extends FrameLayout {

    @InjectView(R.id.store_name_text)
    TextView mStoreNameText;
    @InjectView(R.id.store_distance_text)
    TextView mStoreDistanceText;
    @InjectView(R.id.store_info_click_view)
    View mStoreInfoClickView;
    @InjectView(R.id.item_divider_view)
    View mDividerView;

    public Store mStore;
    private StoresView.Listener mListener;

    public StoreItemView(Context context) {
        super(context);
        initSectionItemView();
    }

    public StoreItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initSectionItemView();
    }

    public StoreItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initSectionItemView();
    }

    private void initSectionItemView() {
        LayoutInflater.from(getContext()).inflate(R.layout.store_item_view, this);
        ButterKnife.inject(this);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                getResources().getDimensionPixelSize(R.dimen.store_item_height));
        setLayoutParams(params);

        FrameLayout.LayoutParams paramsOfDividerView = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 1);
        paramsOfDividerView.gravity = Gravity.BOTTOM;
        int marginOfDividerView = getResources().getDimensionPixelOffset(
                R.dimen.store_item_divider_margin);
        paramsOfDividerView.setMargins(marginOfDividerView, 0, marginOfDividerView, 0);
        mDividerView.setLayoutParams(paramsOfDividerView);
    }

    public void init(Store store) {
        mStoreNameText.setText(store.name);
        if (!TextUtils.isEmpty(store.distance)) {
            mStoreDistanceText.setText(store.distance);
        } else {
            mStoreDistanceText.setVisibility(View.GONE);
        }
        mStore = store;
        mStoreInfoClickView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onStoreInfoIconClicked(mStore);
                }
            }
        });
    }

    public int getStoreId() {
        return mStore.id;
    }

    public Store getStore(){
        return mStore;
    }
    public void setOnInfoIconClickListener(StoresView.Listener listener) {
        mListener = listener;
    }

    public void setClicked(boolean clicked) {
        mStoreNameText.setTextColor(clicked ? getResources().getColor(R.color.ymx_orange) : 0xff333333);
    }

}
